package mess;

import java.util.Arrays;

public class LeeCode_33 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 3};
        System.out.println(search(nums, 3));
    }

    private static int search(int[] nums, int target) {
        int n = nums.length;
        int l = 0;
        int r = n - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            mid = (l + r) / 2;
            //中点在左半段 eg: 3 4 5 6 0 1 2       4 5 6
            if (nums[mid] > nums[n - 1]) {
                //目标在右边
                if (target > nums[mid] || target <= nums[n - 1])
                    l = mid + 1;
                    //目标在左边
                else
                    r = mid;
            }
            //中点在右半段 eg: 3 4 5 0 1 2 3 4
            else {
                //目标在右
                if (target > nums[mid] && target <= nums[n - 1])
                    l = mid + 1;
                    //目标在左
                else
                    r = mid;
            }
        }
        return nums[l] == target ? l : -1;
    }
}


